<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 卡车上的最大单元数 -->
    <script>
      function maximumUnits(boxTypes, truckSize) {
        let arr = boxTypes.sort((a, b) => b[1] - a[1])
        let sum = 0
        for (let item of arr) {
          let count = item[0]
          while (count && truckSize) {
            sum += item[1]
            count--
            truckSize--
          }
        }
        return sum
      }
      console.log(
        maximumUnits(
          [
            [1, 3],
            [2, 2],
            [3, 1]
          ],
          4
        )
      )
    </script>
  </body>
</html>
